
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include <SDL/SDL.h>
#include "libraries/BM_init.h"
#include "libraries/BM_input.h"
#include "libraries/BM_time.h"
#include "libraries/BM_menu.h"
#include "libraries/BM_anitex.h"
#include "libraries/BM_level.h"

#define SCREEN_WIDTH  800
#define SCREEN_HEIGHT 600
#define SCREEN_BPP     32
//Global Variables and Structures
SDL_Surface *screen;
/*
//Global Variables and Structures
BMLevel level;
//<<<<<<< .mine
BMKeyboard keyboard;
int  bmGameLoop(){
    BMTimer timer;
    keyboard.poll();
    if(keyboard.press(SDLK_a))
        return 0;
    level.draw();
    timer.frameDelay();
    SDL_GL_SwapBuffers( );
	return 1;
}*/

/* Logs a message into "log.txt"
 * @param const char * the error to output to the console
 * @param args the arguments
*/
void logMessage(const char *message, ...)
{
    char buffer[256];
    FILE * log;
    va_list args;

    va_start (args, *message);
    vsprintf (buffer,message, args);

    log = fopen("./log.txt", "w");
    fprintf(log, buffer);

    fclose(log);
    va_end (args);
}
int use_joystick =0;
int main( int argc, char **argv ){

    int videoFlags;
    SDL_Event event;
    const SDL_VideoInfo *videoInfo;
    if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ){
	    fprintf( stderr, "Video initialization failed: %s\n",
		     SDL_GetError( ) );
	    return 1;
	}
    videoInfo = SDL_GetVideoInfo( );
    if ( !videoInfo ){
	    fprintf( stderr, "Video query failed: %s\n",
		     SDL_GetError( ) );
	    return 1;
	}
    // the flags to pass to SDL_SetVideoMode
    videoFlags  = SDL_OPENGL;          // Enable OpenGL in SDL
    videoFlags |= SDL_GL_DOUBLEBUFFER; // Enable double buffering
    videoFlags |= SDL_HWPALETTE;       // Store the palette in hardware
    //videoFlags |= SDL_RESIZABLE;       // Enable window resizing
    if ( videoInfo->hw_available )
	videoFlags |= SDL_HWSURFACE;
    else
	videoFlags |= SDL_SWSURFACE;
    if ( videoInfo->blit_hw )
	videoFlags |= SDL_HWACCEL;
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,videoFlags );
    if ( !screen ){
	    fprintf( stderr,  "Video mode set failed: %s\n", SDL_GetError( ) );
	    return 1;
	}

	int error = 0;
	
    GLfloat ratio = ( GLfloat )SCREEN_WIDTH / ( GLfloat )SCREEN_HEIGHT;
    glViewport( 0, 0, ( GLsizei )SCREEN_WIDTH, ( GLsizei )SCREEN_HEIGHT );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity( );
    gluPerspective( 45.0f, ratio, 0.1f, 1000.0f );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity( );

    glEnable( GL_TEXTURE_2D );
    glShadeModel( GL_SMOOTH );
    glClearColor( 0.0f, 0.0f, 1.0f, 1.0f );
    glClearDepth( 1.0f );
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
	


	//BMInit initClass;
	//initClass.initGL(SCREEN_WIDTH,SCREEN_HEIGHT);
	
	SDL_ShowCursor(SDL_DISABLE);
	#ifdef WIN32
		SDL_WM_SetCaption( "Black Maze", NULL );
		//SDL_Surface* icon = SDL_LoadBMP("icon.bmp");
		//SDL_WM_SetIcon(icon, NULL);
	#endif

	BMMenu menu;

	//menu.loadObjects();
	menu.run_menu();

	/*SDL_Event event;
	level.load();
	while ( bmGameLoop() ){
	    while ( SDL_PollEvent( &event ) ){
	        if( event.type == SDL_QUIT)
                return 0;
	    }
	}*/

    //Free remaining data
    return 0;
}

